package com.qf.game.service;

import com.qf.framwork.utils.ConnectionUtils;
import com.qf.game.dao.OrdersDao;
import com.qf.game.entity.Orders;

import java.sql.SQLException;
import java.util.List;

/**
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
public class OrdersService {

    private OrdersDao ordersDao = new OrdersDao();

    private PlayerService playerService = new PlayerService();

    public int insertOrders(Orders orders){
        return ordersDao.insertOrders(orders);
    }

    public Orders getOrdersById(Integer oid){
        return ordersDao.getOrdersById(oid);
    }

    public Orders getOrdersByOid(String orderId){
        return ordersDao.getOrdersByOid(orderId);
    }

    /**
     * 修改订单对象
     * @param orders
     * @return
     */
    public int updateOrders(Orders orders) {
        int result = -1;
        try {
            ConnectionUtils.begin();
            result = ordersDao.updateOrders(orders);

            //修改对应用户的欢乐豆数量  0-未支付 1-已支付 2-申请退款 3-退款中 4-已退款
            if (orders.getStatus() == 1) {
                //当前订单支付成功，修改对应的用户欢乐豆数量
                playerService.updatePlayerHappyBean(orders.getHappeyBean(), 0, orders.getPid());
            }
            ConnectionUtils.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            ConnectionUtils.rollback();
        }
        return result;
    }

    /**
     * 根据用户id查询订单列表
     * @param pid
     * @return
     */
    public List<Orders> queryOrderList(Integer pid){
        return ordersDao.queryOrderList(pid);
    }
}
